<?php
namespace common\records;
use Yii;
use yii\db\ActiveRecord;

class Order extends ActiveRecord
{
    /**
    ** @return string 返回该AR类关联的数据表名
    **/
    public static function tableName()
    {
        return 'order_info';
    }

    public function rules()
    {
        return [
            [['uid','machine_id'],'required'],
            [['machine_id','uid'], 'string'],
            [['status','coin'], 'integer'],
            ];
    }

    public function attributeLabels()
    {
        return [
            'order_id' => '订单ID',
            'uid' => '下单用户',
            'machine_id' => '机器唯一标识',
            'timestamp' => '添加时间',
            'coin' => '需要娃娃币',
            'status' => '状态',
            ];
    }

    public function getUser()
    {
        return $this->hasOne(\common\records\UserInfo::className(), ['uid' => 'uid']);
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert == true) {
                $this->timestamp = date('Y-m-d H:i:s');
            }
            return true;
        }
        return false;
    }
}
